package udpserver;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JTextArea;


public class ServerMonitorFrame extends JFrame{
    Server server;
    TrafficMeter trafficMeter;
    JTextArea taServerStatus;

    public ServerMonitorFrame(String label, Server server){
        super(label);
        this.server = server;
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        trafficMeter = new TrafficMeter(server);
        taServerStatus = new JTextArea();
        taServerStatus.setBackground(Color.BLACK);
        taServerStatus.setPreferredSize(new Dimension(trafficMeter.width, 100));
        taServerStatus.setEditable(false);
        taServerStatus.setForeground(Color.RED);
        this.add(trafficMeter, BorderLayout.NORTH);
        this.add(taServerStatus, BorderLayout.SOUTH);
        this.pack();
        this.setVisible(true);
        new Thread(trafficMeter).start();
    }

    public void setStatus(){
        taServerStatus.setText("");
        taServerStatus.append("online: " + server.clients.size() + "\n");
        for (int i = 0; i < server.clients.size(); i++) {
            Client c = server.clients.get(i);
            taServerStatus.append(c.nick + "(id" + c.id + "), ");
        }
        taServerStatus.append("\n\n");
        taServerStatus.append("total traffic out: " + server.trafficOutTotal + "\n");
        taServerStatus.append("total traffic in: " + server.trafficInTotal + "\n");
        taServerStatus.append("recieved packets: " + server.numberOfPackets);
    }

}
